
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;


public class FactoryData extends JFrame{
    public FactoryData(String mUID){

        SQLOperation sql=new SQLOperation();
        setTitle("Factory Data");
        Container container=getContentPane();
        setLayout(new BorderLayout());


        JPanel titlePanel=new JPanel();
        JLabel title=new JLabel("Select the product you'd like to analysis.");
        title.setFont(new Font("",Font.BOLD,20));
        titlePanel.add(title);
        add(titlePanel,BorderLayout.NORTH);

        JPanel comboPanel=new JPanel();

        String[] deviceAttri={"idDevice","DeviceType_typeID","Family_familyID"};

        Object[][] deviceData=sql.search("Device",deviceAttri,"Manufacture_mUID",mUID);

        JComboBox selectProduct=new JComboBox();

        selectProduct.addItem("----------------");
        int count=0;
        for(int i=0;i<deviceData.length;i++) {

            if((!deviceData[i][2].equals("0"))&&(!deviceData[i][2].equals(""))){
                selectProduct.addItem(deviceData[i][0]);
            }else{
                count++;
            }
        }

        comboPanel.add(selectProduct);

        add(comboPanel,BorderLayout.CENTER);


        JPanel buttons=new JPanel();

        JButton back=new JButton("Back");
        back.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                setVisible(false);
                new FactoryDefaultFrame(mUID);
            }
        });
        buttons.add(back);
        int k=count;
        JButton enter=new JButton("Select");
        enter.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if(selectProduct.getSelectedIndex()==0){
                    JOptionPane.showMessageDialog
                            (null, "Please select a product!","Empty select",JOptionPane.ERROR_MESSAGE);

                }
                else{
                    //Go list data.
                    Object[][] detailData;
                    int num=-1;
                    String[] dataBelongs={"Lamps","WindowsDoors","Humidity","Temperature"};
                    String[][] dataInfo={{"isOn","timeStamp"},
                            {"isOpen","timeStamp"},
                            {"hygro","timeStamp"},
                            {"tempNum","timeStamp"}};
                    Object[][] searchData;
                    for (int i = 0; i < 4; i++) {
                        searchData = sql.search(dataBelongs[i], dataInfo[i],
                                "Device_idDevice", selectProduct.getSelectedItem());
                        if(searchData.length!=0){
                            num=i;break;
                        }

                    }


                    if(num!=-1){new FactoryEquipList(num,(String)selectProduct.getSelectedItem());}
                    else{
                        JOptionPane.showMessageDialog
                                (null, "Empty Data!","Empty data",JOptionPane.ERROR_MESSAGE);

                    }
                }
            }
        });
        buttons.add(enter);

        add(buttons,BorderLayout.SOUTH);

        setResizable(false);
        setVisible(true);
        setSize(420,200);
        setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);






    }
}
